package com.base.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.base.common.core.page.TableDataInfo;
import com.base.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.system.domain.bo.TSysUserCertBo;
import com.base.system.domain.vo.TSysUserCertVo;
import com.base.system.mapper.TSysUserCertMapper;
import com.base.system.service.ITSysUserCertService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.base.common.utils.StringUtils;
import com.base.system.domain.TSysUserCert;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 系统用户证书Service业务层处理
 *
 * @author base
 * @date 2023-10-18
 */
@RequiredArgsConstructor
@Service
public class TSysUserCertServiceImpl implements ITSysUserCertService {

    private final TSysUserCertMapper baseMapper;

    /**
     * 查询系统用户证书
     */
    @Override
    public TSysUserCertVo queryById(String autoId){
        return baseMapper.selectVoById(autoId);
    }

    /**
     * 查询系统用户证书列表
     */
    @Override
    public TableDataInfo<TSysUserCertVo> queryPageList(TSysUserCertBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TSysUserCert> lqw = buildQueryWrapper(bo);
        Page<TSysUserCertVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询系统用户证书列表
     */
    @Override
    public List<TSysUserCertVo> queryList(TSysUserCertBo bo) {
        LambdaQueryWrapper<TSysUserCert> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TSysUserCert> buildQueryWrapper(TSysUserCertBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TSysUserCert> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSuperAdmin()), TSysUserCert::getSuperAdmin, bo.getSuperAdmin());
        lqw.eq(StringUtils.isNotBlank(bo.getUserId()), TSysUserCert::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getCertSn()), TSysUserCert::getCertSn, bo.getCertSn());
        lqw.eq(StringUtils.isNotBlank(bo.getCertStatus()), TSysUserCert::getCertStatus, bo.getCertStatus());
        return lqw;
    }

    /**
     * 新增系统用户证书
     */
    @Override
    public Boolean insertByBo(TSysUserCertBo bo) {
        TSysUserCert add = BeanUtil.toBean(bo, TSysUserCert.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAutoId(add.getAutoId());
        }
        return flag;
    }

    /**
     * 修改系统用户证书
     */
    @Override
    public Boolean updateByBo(TSysUserCertBo bo) {
        TSysUserCert update = BeanUtil.toBean(bo, TSysUserCert.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TSysUserCert entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除系统用户证书
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
